package cn.com.yx.ms.controller;

import cn.com.yx.ms.application.web.ApiQuery;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.TestModel;
import cn.com.yx.ms.domain.mq.MessageBase;
import cn.com.yx.ms.domain.mq.MqCommandType;
import cn.com.yx.ms.domain.mq.upstream.U_Message3001;
import cn.com.yx.ms.domain.template.LineBusModel;
import cn.com.yx.ms.domain.template.LineBusyModel;
import cn.com.yx.ms.domain.template.LineModel;
import cn.com.yx.ms.service.IExternalDataService;
import cn.com.yx.ms.service.ITestService;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.configuration.BaseConstant;
import cn.yx.framework.security.Des3Kit;
import cn.yx.framework.security.MD5Util;
import cn.yx.framework.security.RSAKit;
import cn.yx.framework.system.SystemUtil;
import cn.yx.framework.util.DateUtil;
import cn.yx.framework.view.ApiResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * 应用中心
 */
@RestController
@RequestMapping(value = "externaldata")
@Api(value = "外部数据接口", description = "用于用于获取外部各种数据")
public class ExternalDataController {

    private static final Logger logger = LoggerFactory.getLogger(ExternalDataController.class);

    @Resource
    private IExternalDataService iExternalDataService;

    @ResponseBody
    @RequestMapping(value = "busdata/{devid}/5011", method = RequestMethod.GET)
    @ApiOperation("公交线路数据")
    public MessageBase<List<LineModel>> find5011(@PathVariable("devid") String devid) {
        String json = iExternalDataService.getJson(devid, MqCommandType.C5001);
        MessageBase<List<LineModel>> line = JSON.parseObject(json, new TypeReference<MessageBase<List<LineModel>>>() {
        });
        return line;
    }

    @ResponseBody
    @RequestMapping(value = "busdata/{devid}/5012", method = RequestMethod.GET)
    @ApiOperation("公交线路数据")
    public MessageBase<List<LineBusyModel>> find5012(@PathVariable("devid") String devid) {
        String json = iExternalDataService.getJson(devid, MqCommandType.C5002);
        MessageBase<List<LineBusyModel>> line = JSON.parseObject(json, new TypeReference<MessageBase<List<LineBusyModel>>>() {
        });
        return line;
    }

    @ResponseBody
    @RequestMapping(value = "busdata/{devid}/5013", method = RequestMethod.GET)
    @ApiOperation("小车数据")
    public MessageBase<List<LineBusModel>> find5013(@PathVariable("devid") String devid) {
        String json = iExternalDataService.getJson(devid, MqCommandType.C5003);

        /* 添加首页统计缓存 */
        iExternalDataService.setWebSocketBusData(devid);

        MessageBase<List<LineBusModel>> line = JSON.parseObject(json, new TypeReference<MessageBase<List<LineBusModel>>>() {
        });
        return line;
    }

    @ResponseBody
    @RequestMapping(value = "busdata/{devid}/{command}", method = RequestMethod.GET)
    @ApiOperation("公交数据H5调用")
    public ApiResult findBusData(@PathVariable("devid") String devid, @PathVariable("command") String command) {
        String json = iExternalDataService.getJson(devid, MqCommandType.getEnumFromString(command));
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, null, json);

    }
}